<template>
	<view class="ljapp my-doctor">
		<view class="header u-flex u-flex-around">
			<view class="item" :class="{on:tab==0}" @click="changeTab(0)">我的收藏<text v-if="followNum>0">（{{followNum}}）</text></view>
			<view class="item" :class="{on:tab==1}" @click="changeTab(1)">签约医生<text v-if="signNum>0">（{{signNum}}）</text></view>
		</view>
		
		<view class="content">
			<view class="list">
				<view class="item u-flex" :class="tab==1?item.status:''" @click="Oninquiry(item)" v-for="(item,index) in group.list" :key="index">
					<image class="avatar" :src="$base.cdn(item.doctor_info?item.doctor_info.img:item.doctor_img,'/static/lj/common/default-doctor.png')" mode="aspectFill"></image>
					<view class="info u-flex-fill">
						<view class="tit u-flex u-flex-between">
							<view class="name">{{item.doctor_info?item.doctor_info.name:item.doctor_name}}</view>
							<view class="btn-capsule" v-if="tab==0" @click.stop="cancelFollow(item,index)">取消关注</view>
							<view :class="[item.status === 'patient' ? 'tag' : 'status']">{{$u.timeFormat(item.createtime, 'yyyy.mm.dd hh:MM:ss')}}<text>签约</text></view>
							<!-- <view class="tag" v-else>{{$u.timeFormat(item.createtime,'yyyy.mm.dd hh:MM:ss')}}<text>签约</text></view> -->
						</view>
						<view class="con u-line-2">
							擅长：{{item.doctor_info?item.doctor_info.be_good_at_synopsis:''}}
						</view>
						<view class="time u-flex u-flex-between" v-if="tab==1">
							<view>有效期：{{$u.timeFormat(item.starttime,'yyyy.mm.dd')}}-{{$u.timeFormat(item.endtime,'yyyy.mm.dd')}}</view>
							<view class="status">{{item.typename}}</view>
						</view>
					</view>
				</view>
			</view>
			
			<lj-empty v-if="group.loaded&&group.list.length<=0">暂无{{tab==0?'收藏':'签约'}}医生</lj-empty>
			<view v-else>
				<u-loadmore
					:status="loading?'loading':(group.loaded?'nomore':'loadmore')"
					fontSize="28rpx"
					color="#d9d9d9"
					nomoreText="已加载全部"
					marginTop="30rpx"
					marginBottom="0"
					@loadmore="getList"
				></u-loadmore>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tab: 0,
				followNum: 0,
				signNum: 0,
				
				group:{
					page: 1,
					limit: 10,
					list: [],
					loaded: false,
				},
				loading: false,
			}
		},
		onLoad() {},
		onShow(){
			this.getMydoctorCount()
			this.init()
		},
		methods: {
			init(){
				this.group.page = 1
				this.group.list = []
				this.group.loaded = false
				this.loading = false
			
				this.getList();
			},
			getMydoctorCount(){
				this.$Http({
					url: 'getMydoctorCount',
				}).then(res=>{
					this.followNum = res.data.followNum
					this.signNum = res.data.signNum
				})
			},
			getList(){
				var that = this, group = that.group, flagTab = that.tab;
				if(that.loading || group.loaded) return;
			
				that.loading = true
			
				this.$Http({
					url: that.tab==0?'getMyFollowDoctorList':'getMySignDoctorList',
					data: {
						page: group.page,
						limit: group.limit,
					},
					type:'POST',
				}).then(res=>{
					if(res.code == 1 && flagTab==that.tab){
						var list = res.data.data;
						group.loaded = list.length < group.limit;
						group.page ++;
						
						if(that.tab==1){
							list.forEach(item => {
								if (item.pay_status == 2) {
									if (item.status == 'normal') {
										item.typename = '待支付'
									}
									if (item.status == 'cancel') {
										item.typename = '已取消'
									}
									item.status_is = false
								}
								if (item.pay_status == 1) {
									item.status_is = true
									if (item.status == 'normal') {
										item.typename = '待接诊'
									}
									if (item.status == 'refund') {
										item.typename = '已退款'
										item.status_is = false
									}
									if (item.status == 'refuse_patient') {
										item.typename = '拒绝接诊'
									}
									if (item.status == 'patient') {
										item.typename = '服务中'
									}
									if (item.status == 'end') {
										item.typename = '已失效'
									}
									if (item.status == 'comment') {
										item.typename = '已评价'
									}
								}
							})
						}
						
						group.list = group.list.concat(list);
					}
					
					that.loading = false
				}).catch(()=>{
					that.loading = false
				});
			},
			onReachBottom(){
				this.getList()
			},
			changeTab(e){
				if(this.tab==e) return false
				this.tab = e
				this.init()
			},
			
			cancelFollow(res,index) {
				this.$Http({
					url: 'deladdUserDoctor',
					data: {
						doctor_id: res.doctor_id,
					},
					show: true,
					type: 'POST'
				}).then(res => {
					if (res.code == 1) {
						this.group.list.splice(index,1)
						this.followNum--
					}
				})
			},
			//医生详情页
			Oninquiry(res) {
				uni.navigateTo({
					url: '/pagesA/ask/inquiry?id=' + res.doctor_id,
				})
			},
		}
	}
</script>

<style lang="less">
	@import '@/pagesL/style.less';
</style>
